package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import pojo.User;
import util.DBUtil;
import dao.UserDao;

public class UserDaoImpl implements UserDao{

	Connection conn;
	PreparedStatement ps;
	
	

	@Override
	public String addUser(User user) {
		conn = getConnection();
		String sql = "insert into users(id, uname, psd) values(?,?,?)";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);

			if(user.getUid()==null)
				user.setUid(UUID.randomUUID().toString());
			ps.setString(1, user.getUid());
			ps.setString(2, user.getUname());
			ps.setString(3, user.getPsd());

			int m = ps.executeUpdate();
			conn.commit();
			ps.close();
			conn.close();
			return user.getUid();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
				ps.close();
				conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return null;
		}
	}

	@Override
	public int updateUser(User user) {
		conn = getConnection();
		String sql = "update users set uname=?,psd=? where id=?";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);


			ps.setString(1, user.getUname());
			ps.setString(2, user.getPsd());
			ps.setString(3, user.getUid());

			int m = ps.executeUpdate();
			conn.commit();
			ps.close();
			conn.close();
			return m;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
				ps.close();
				conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return 0;
		}
	}

	@Override
	public int deleteUser(String uid) {
		conn = getConnection();
		String sql = "delete users where id=?";
		try {
			ps = conn.prepareStatement(sql);

			ps.setString(1,uid);
			int m = ps.executeUpdate();
			ps.close();
			conn.close();
			return m;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				ps.close();conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			return 0;
		}
	}

	@Override
	public User searchUser(String uid) {
		conn = getConnection();
		String sql = "select * from users where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, uid);
			ResultSet set = ps.executeQuery();
			if(set.next()) {
				User user = new User();
				user.setUid(set.getString("id"));
				user.setUname(set.getString("uname"));
				user.setPsd(set.getString("psd"));
				ps.close();
				conn.close();
				return user;
			}
			ps.close();
			conn.close();
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				ps.close();conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			return null;
		}
	}

	@Override
	public List<User> findAllUsers() {
		conn = getConnection();
		List<User> list = new ArrayList<User>();
		String sql = "select * from users";
		try {
			ps = conn.prepareStatement(sql);
			ResultSet set = ps.executeQuery();
			while (set.next()) {
				User user = new User();
				user.setUid(set.getString("id"));
				user.setUname(set.getString("uname"));
				user.setPsd(set.getString("psd"));
				list.add(user);
			}
			ps.close();
			conn.close();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				ps.close();
				conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			return null;
		}
	}
	
	private Connection getConnection() {
		try {
			InitialContext cxt = new InitialContext();// / JNDI 树
			DataSource ds = (DataSource) cxt
					.lookup("java:/comp/env/jdbc/myoracle");// // 查找 [tomcat
															// 协议]/jdbc/xxxx
			Connection conn = ds.getConnection();
			// System.out.println(conn);
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
//		return DBUtil.createConnection();
		
	}

	@Override
	public User searchUser(String uname, String psd) {
		conn = getConnection();
		String sql = "select * from users where uname=? and psd=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, uname);
			ps.setString(2, psd);
			ResultSet set = ps.executeQuery();
			if(set.next()) {
				User user = new User();
				user.setUid(set.getString("id"));
				user.setUname(set.getString("uname"));
				user.setPsd(set.getString("psd"));
				ps.close();
				conn.close();
				return user;
			}
			ps.close();
			conn.close();
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				ps.close();conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			return null;
		}
	}
}
